var my_store_id = getQueryString('mystore_id');
var store_id;
var initPhotoSwipeFromDOM = function(gallerySelector) {

    // 解析来自DOM元素幻灯片数据（URL，标题，大小...）
    // (children of gallerySelector)
    var parseThumbnailElements = function(el) {
        var thumbElements = el.childNodes,
            numNodes = thumbElements.length,
            items = [],
            figureEl,
            linkEl,
            size,
            item,
            divEl;

        for(var i = 0; i < numNodes; i++) {

            figureEl = thumbElements[i]; // <figure> element

            // 仅包括元素节点
            if(figureEl.nodeType !== 1) {
                continue;
            }
            divEl = figureEl.children[0];
            linkEl = divEl.children[0]; // <a> element

            size = linkEl.getAttribute('data-size').split('x');

            // 创建幻灯片对象
            item = {
                src: linkEl.getAttribute('href'),
                w: parseInt(size[0], 10),
                h: parseInt(size[1], 10)
            };



            if(figureEl.children.length > 1) {
                // <figcaption> content
                item.title = figureEl.children[1].innerHTML;
            }

            if(linkEl.children.length > 0) {
                // <img> 缩略图节点, 检索缩略图网址
                item.msrc = linkEl.children[0].getAttribute('src');
            }

            item.el = figureEl; // 保存链接元素 for getThumbBoundsFn
            items.push(item);
        }

        return items;
    };

    // 查找最近的父节点
    var closest = function closest(el, fn) {
        return el && ( fn(el) ? el : closest(el.parentNode, fn) );
    };

    // 当用户点击缩略图触发
    var onThumbnailsClick = function(e) {
        e = e || window.event;
        e.preventDefault ? e.preventDefault() : e.returnValue = false;

        var eTarget = e.target || e.srcElement;

        // find root element of slide
        var clickedListItem = closest(eTarget, function(el) {
            return (el.tagName && el.tagName.toUpperCase() === 'FIGURE');
        });

        if(!clickedListItem) {
            return;
        }

        // find index of clicked item by looping through all child nodes
        // alternatively, you may define index via data- attribute
        var clickedGallery = clickedListItem.parentNode,
            childNodes = clickedListItem.parentNode.childNodes,
            numChildNodes = childNodes.length,
            nodeIndex = 0,
            index;

        for (var i = 0; i < numChildNodes; i++) {
            if(childNodes[i].nodeType !== 1) {
                continue;
            }

            if(childNodes[i] === clickedListItem) {
                index = nodeIndex;
                break;
            }
            nodeIndex++;
        }



        if(index >= 0) {
            // open PhotoSwipe if valid index found
            openPhotoSwipe( index, clickedGallery );
        }
        return false;
    };

    // parse picture index and gallery index from URL (#&pid=1&gid=2)
    var photoswipeParseHash = function() {
        var hash = window.location.hash.substring(1),
            params = {};

        if(hash.length < 5) {
            return params;
        }

        var vars = hash.split('&');
        for (var i = 0; i < vars.length; i++) {
            if(!vars[i]) {
                continue;
            }
            var pair = vars[i].split('=');
            if(pair.length < 2) {
                continue;
            }
            params[pair[0]] = pair[1];
        }

        if(params.gid) {
            params.gid = parseInt(params.gid, 10);
        }

        return params;
    };

    var openPhotoSwipe = function(index, galleryElement, disableAnimation, fromURL) {
        var pswpElement = document.querySelectorAll('.pswp')[0],
            gallery,
            options,
            items;

        items = parseThumbnailElements(galleryElement);

        // 这里可以定义参数
        options = {
            barsSize: {
                top: 100,
                bottom: 100
            },
            fullscreenEl : false,
            shareButtons: [
                {id:'wechat', label:'分享微信', url:'#'},
                {id:'weibo', label:'新浪微博', url:'#'},
                {id:'download', label:'保存图片', url:'{{raw_image_url}}', download:true}
            ],

            // define gallery index (for URL)
            galleryUID: galleryElement.getAttribute('data-pswp-uid'),

            getThumbBoundsFn: function(index) {
                // See Options -> getThumbBoundsFn section of documentation for more info
                var thumbnail = items[index].el.getElementsByTagName('img')[0], // find thumbnail
                    pageYScroll = window.pageYOffset || document.documentElement.scrollTop,
                    rect = thumbnail.getBoundingClientRect();

                return {x:rect.left, y:rect.top + pageYScroll, w:rect.width};
            }

        };

        // PhotoSwipe opened from URL
        if(fromURL) {
            if(options.galleryPIDs) {
                // parse real index when custom PIDs are used
                for(var j = 0; j < items.length; j++) {
                    if(items[j].pid == index) {
                        options.index = j;
                        break;
                    }
                }
            } else {
                // in URL indexes start from 1
                options.index = parseInt(index, 10) - 1;
            }
        } else {
            options.index = parseInt(index, 10);
        }

        // exit if index not found
        if( isNaN(options.index) ) {
            return;
        }

        if(disableAnimation) {
            options.showAnimationDuration = 0;
        }

        // Pass data to PhotoSwipe and initialize it
        gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
        gallery.init();
    };

    // loop through all gallery elements and bind events
    var galleryElements = document.querySelectorAll( gallerySelector );

    for(var i = 0, l = galleryElements.length; i < l; i++) {
        galleryElements[i].setAttribute('data-pswp-uid', i+1);
        galleryElements[i].onclick = onThumbnailsClick;
    }

    // Parse URL and open gallery if it contains #&pid=3&gid=1
    var hashData = photoswipeParseHash();
    if(hashData.pid && hashData.gid) {
        openPhotoSwipe( hashData.pid ,  galleryElements[ hashData.gid - 1 ], true, true );
    }
};

var unixTimeToDateString = function(ts, ex) {
    ts = parseFloat(ts) || 0;
    if (ts < 1) {
        return '';
    }
    var d = new Date();
    d.setTime(ts * 1e3);
    var s = '' + d.getFullYear() + '-' + (1 + d.getMonth()) + '-' + d.getDate();
    if (ex) {
        s += ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();
    }
    return s;
};

var buyLimitation = function(a, b) {
    a = parseInt(a) || 0;
    b = parseInt(b) || 0;
    var r = 0;
    if (a > 0) {
        r = a;
    }
    if (b > 0 && r > 0 && b < r) {
        r = b;
    }
    return r;
};

template.helper('isEmpty', function(o) {
    for (var i in o) {
        return false;
    }
    return true;
});


$(function() {
    var goods_id = getQueryString("goods_id")
    get_detail(goods_id);
    $.sValid.init({
        rules:{
            buynum:"digits"
        },
        messages:{
            buynum:"请输入正确的数字"
        },
        callback:function (eId,eMsg,eRules){
            if(eId.length >0){
                var errorHtml = "";
                $.map(eMsg,function (idx,item){
                    errorHtml += "<p>"+idx+"</p>";
                });
                $.sDialog({
                    skin:"red",
                    content:errorHtml,
                    okBtn:false,
                    cancelBtn:false
                });
            }
        }
    });
    //检测商品数目是否为正整数
    function buyNumer(){
        $.sValid();
    }
    function get_detail(goods_id) {
        $.ajax({
            url: ApiUrl + "/index.php?act=goods&op=goods_body",
            data: {goods_id: goods_id},
            type: "get",
            success: function (result) {
                var key = getCookie('key');//登录标记
                var res = eval('(' + result + ')');
                var data = res.datas;
                if (data.goods_info.mobile_body) {
                    $(".fixed-tab-pannel").html(data.goods_info.mobile_body);
                    _initImage();
                }

                function show_tip() {
                    var flyer = $('.goods-pic > img').clone().css({
                        'z-index': '999',
                        'height': '3rem',
                        'width': '3rem'
                    });
                    flyer.fly({
                        start: {
                            left: $('.goods-pic > img').offset().left,
                            top: $('.goods-pic > img').offset().top - $(window).scrollTop()
                        },
                        end: {
                            left: $("#cart_count1").offset().left + 40,
                            top: $("#cart_count1").offset().top - $(window).scrollTop(),
                            width: 0,
                            height: 0
                        },
                        onEnd: function () {
                            flyer.remove();
                        }
                    });
                }

                if(!data.error){
                    //商品图片格式化数据
                    if(data.goods_image){
                        var goods_image = data.goods_image.split(",");
                        data.goods_image = goods_image;
                    }else{
                        data.goods_image = [];
                    }
                    //商品规格格式化数据
                    if(data.goods_info.spec_name){
                        var goods_map_spec = $.map(data.goods_info.spec_name,function (v,i){
                            var goods_specs = {};
                            goods_specs["goods_spec_id"] = i;
                            goods_specs['goods_spec_name']=v;
                            if(data.goods_info.spec_value){
                                $.map(data.goods_info.spec_value,function(vv,vi){
                                    if(i == vi){
                                        goods_specs['goods_spec_value'] = $.map(vv,function (vvv,vvi){
                                            var specs_value = {};
                                            specs_value["specs_value_id"] = vvi;
                                            specs_value["specs_value_name"] = vvv;
                                            return specs_value;
                                        });
                                    }
                                });
                                return goods_specs;
                            }else{
                                data.goods_info.spec_value = [];
                            }
                        });
                        data.goods_map_spec = goods_map_spec;
                    }else {
                        data.goods_map_spec = [];
                    }

                    // 虚拟商品限购时间和数量
                    if (data.goods_info.is_virtual == '1') {
                        data.goods_info.virtual_indate_str = unixTimeToDateString(data.goods_info.virtual_indate, true);
                        data.goods_info.buyLimitation = buyLimitation(data.goods_info.virtual_limit, data.goods_info.upper_limit);
                    }

                    // 预售发货时间
                    if (data.goods_info.is_presell == '1') {
                        data.goods_info.presell_deliverdate_str = unixTimeToDateString(data.goods_info.presell_deliverdate);
                    }


                    if (data.goods_info.is_virtual == '0') {
                        $('.goods-detail-o2o').remove();
                    }

                    //渲染模板
                    var html = template.render('product_detail', data);
                    $("#buy").html(html);

                    //渲染模板
                    var html = template.render('product_detail_sepc', data);
                    $("#product_detail_spec_html").html(html);


                    if (data.goods_info.is_virtual == '1') {
                        store_id = data.store_info.store_id;
                        virtual();
                    }

                    // 购物车中商品数量
                    if (getCookie('cart_count')) {
                        if (getCookie('cart_count') > 0) {
                            $('#cart_count,#cart_count1').html('<sup>'+getCookie('cart_count')+'</sup>');
                        }
                    }

                    //规格属性
                    var myData = {};
                    myData["spec_list"] = data.spec_list;
                    $(".spec a").click(function (){
                        var self = this;
                        arrowClick(self,myData);
                    });
                    //购买数量，减
                    $(".minus").click(function (){
                        var buynum = $(".buy-num").val();
                        if(buynum >1){
                            $(".buy-num").val(parseInt(buynum-1));
                        }
                    });
                    //购买数量加
                    $(".add").click(function (){
                        var buynum = parseInt($(".buy-num").val());
                        if(buynum < data.goods_info.goods_storage){
                            $(".buy-num").val(parseInt(buynum+1));
                        }
                    });
                    // 一个F码限制只能购买一件商品 所以限制数量为1
                    if (data.goods_info.is_fcode == '1') {
                        $('.minus').hide();
                        $('.add').hide();
                        $(".buy-num").attr('readOnly', true);
                    }
                    //加入购物车
                    $("#add-cart").click(function (){
                        var key = getCookie('key');//登录标记
                        var quantity = parseInt($(".buy-num").val());
                        if(!key){
                            var goods_info = decodeURIComponent(getCookie('goods_cart'));
                            if (goods_info == null) {
                                goods_info = '';
                            }
                            if(goods_id<1){
                                show_tip();
                                return false;
                            }
                            var cart_count = 0;
                            if(!goods_info){
                                goods_info = goods_id+','+quantity;
                                cart_count = 1;
                            }else{
                                var goodsarr = goods_info.split('|');
                                for (var i=0; i<goodsarr.length; i++) {
                                    var arr = goodsarr[i].split(',');
                                    if(contains(arr,goods_id)){
                                        show_tip();
                                        return false;
                                    }
                                }
                                goods_info+='|'+goods_id+','+quantity;
                                cart_count = goodsarr.length;
                            }
                            // 加入cookie
                            addCookie('goods_cart',goods_info);
                            // 更新cookie中商品数量
                            addCookie('cart_count',cart_count);
                            show_tip();
                            getCartCount();
                            $('#cart_count,#cart_count1').html('<sup>'+cart_count+'</sup>');
                            return false;
                        }else{
                            $.ajax({
                                url:ApiUrl+"/index.php?act=member_cart&op=cart_add",
                                data:{key:key,goods_id:goods_id,quantity:quantity},
                                type:"post",
                                success:function (result){
                                    var rData = $.parseJSON(result);
                                    if(checkLogin(rData.login)){
                                        if(!rData.datas.error){
                                            show_tip();
                                            // 更新购物车中商品数量
                                            delCookie('cart_count');
                                            getCartCount();
                                        }else{
                                            $.sDialog({
                                                skin:"red",
                                                content:rData.datas.error,
                                                okBtn:false,
                                                cancelBtn:false
                                            });
                                        }
                                    }
                                }
                            })
                        }
                    });
                    function virtual() {
                        $('#get_area_selected').parents('.goods-detail-item').remove();
                        $.getJSON(ApiUrl + '/index.php?act=goods&op=store_o2o_addr', {store_id:store_id},function(result){
                            if (!result.datas.error) {
                                if (result.datas.addr_list.length > 0) {
                                    $('#list-address-ul').html(template.render('list-address-script',result.datas));
                                    map_list = result.datas.addr_list;
                                    var _html = '';
                                    _html += '<dl index_id="0">';
                                    _html += '<dt>'+ map_list[0].name_info +'</dt>';
                                    _html += '<dd>'+ map_list[0].address_info +'</dd>';
                                    _html += '</dl>';
                                    _html += '<p><a href="tel:'+ map_list[0].phone_info +'"></a></p>';
                                    $('#goods-detail-o2o').html(_html);

                                    $('#goods-detail-o2o').on('click','dl',map);

                                    if (map_list.length > 1) {
                                        $('#store_addr_list').html('查看全部'+map_list.length+'家分店地址');
                                    } else {
                                        $('#store_addr_list').html('查看商家地址');
                                    }
                                    $('#map_all > em').html(map_list.length);
                                } else {
                                    $('.goods-detail-o2o').hide();
                                }
                            }
                        });
                        $.animationLeft({
                            valve : '#store_addr_list',
                            wrapper : '#list-address-wrapper',
                            scroll : '#list-address-scroll'
                        });
                    }
                    //立即购买
                    if (data.goods_info.is_virtual == '1') {
                        $("#buy-now").click(function() {
                            var key = getCookie('key');//登录标记
                            if (!key) {
                                window.location.href = WapSiteUrl+'/tmpl/member/login.html';
                                return false;
                            }
                            var buynum = parseInt($('.buy-num').val()) || 0;
                            if (buynum < 1) {
                                $.sDialog({
                                    skin:"red",
                                    content:'参数错误！',
                                    okBtn:false,
                                    cancelBtn:false
                                });
                                return;
                            }
                            if (buynum > data.goods_info.goods_storage) {
                                $.sDialog({
                                    skin:"red",
                                    content:'库存不足！',
                                    okBtn:false,
                                    cancelBtn:false
                                });
                                return;
                            }

                            // 虚拟商品限购数量
                            if (data.goods_info.buyLimitation > 0 && buynum > data.goods_info.buyLimitation) {
                                $.sDialog({
                                    skin:"red",
                                    content:'超过限购数量！',
                                    okBtn:false,
                                    cancelBtn:false
                                });
                                return;
                            }

                            var json = {};
                            if(my_store_id){
                                json.my_store_id = my_store_id;
                            }
                            json.key = key;
                            json.cart_id = goods_id;
                            json.quantity = buynum;
                            $.ajax({
                                type:'post',
                                url:ApiUrl+'/index.php?act=member_vr_buy&op=buy_step1',
                                data:json,
                                dataType:'json',
                                success:function(result){
                                    if (result.datas.error) {
                                        $.sDialog({
                                            skin:"red",
                                            content:result.datas.error,
                                            okBtn:false,
                                            cancelBtn:false
                                        });
                                    } else {
                                        if(my_store_id){
                                            location.href = WapSiteUrl+'/tmpl/order/vr_buy_step1.html?goods_id='+goods_id+'&quantity='+buynum+"&my_store_id="+my_store_id;
                                        }else{
                                            location.href = WapSiteUrl+'/tmpl/order/vr_buy_step1.html?goods_id='+goods_id+'&quantity='+buynum;
                                        }

                                    }
                                }
                            });
                        });
                    } else {
                        $("#buy-now").click(function (){
                            var key = getCookie('key');//登录标记
                            if(!key){
                                window.location.href = WapSiteUrl+'/tmpl/member/login.html';
                            }else{
                                var buynum = parseInt($('.buy-num').val()) || 0;

                                if (buynum < 1) {
                                    $.sDialog({
                                        skin:"red",
                                        content:'参数错误！',
                                        okBtn:false,
                                        cancelBtn:false
                                    });
                                    return;
                                }
                                if (buynum > data.goods_info.goods_storage) {
                                    $.sDialog({
                                        skin:"red",
                                        content:'库存不足！',
                                        okBtn:false,
                                        cancelBtn:false
                                    });
                                    return;
                                }
                                var json = {};
                                json.key = key;
                                json.cart_id = goods_id+'|'+buynum;
                                $.ajax({
                                    type:'post',
                                    url:ApiUrl+'/index.php?act=member_buy&op=buy_step1',
                                    data:json,
                                    dataType:'json',
                                    success:function(result){
                                        if (result.datas.error) {
                                            $.sDialog({
                                                skin:"red",
                                                content:result.datas.error,
                                                okBtn:false,
                                                cancelBtn:false
                                            });
                                        }else{
                                            if(my_store_id){
                                                location.href = WapSiteUrl+'/tmpl/order/buy_step1.html?goods_id='+goods_id+'&buynum='+buynum+"&my_store_id="+my_store_id;
                                            }else{
                                                location.href = WapSiteUrl+'/tmpl/order/buy_step1.html?goods_id='+goods_id+'&buynum='+buynum;
                                            }
                                        }
                                    }
                                });
                            }
                        });

                    }

                }else {

                    $.sDialog({
                        content: data.error + '！<br>请返回上一页继续操作…',
                        okBtn:false,
                        cancelBtnText:'返回',
                        cancelFn: function() { history.back(); }
                    });
                }

                //验证购买数量是不是数字
                $("#buynum").blur(buyNumer);


                // 从下到上动态显示隐藏内容
                $.animationUp({
                    valve : '.animation-up,#goods_spec_selected',          // 动作触发
                    wrapper : '#product_detail_spec_html',    // 动作块
                    scroll : '#product_roll',     // 滚动块，为空不触发滚动
                    start : function(){       // 开始动作触发事件
                        $('.goods-detail-foot').addClass('hide').removeClass('block');
                        $('.nctouch-bottom-mask').addClass('show').removeClass('block');
                    },
                    close : function(){        // 关闭动作触发事件
                        $('.goods-detail-foot').removeClass('hide').addClass('block');
                        $('.nctouch-bottom-mask').removeClass('show').addClass('block');
                    }
                });

                $.animationUp({
                    valve : '#getVoucher',          // 动作触发
                    wrapper : '#voucher_html',    // 动作块
                    scroll : '#voucher_roll',     // 滚动块，为空不触发滚动
                });

                // 联系客服
                $('.kefu').click(function(){
                    window.location.href = WapSiteUrl+'/tmpl/member/chat_info.html?goods_id=' + goods_id + '&t_id=' + data.store_info.member_id;
                })
            }
        });
    }

    //点击商品规格，获取新的商品
    function arrowClick(self,myData){
        $(self).addClass("current").siblings().removeClass("current");
        //拼接属性
        var curEle = $(".spec").find("a.current");
        var curSpec = [];
        $.each(curEle,function (i,v){
            // convert to int type then sort
            curSpec.push(parseInt($(v).attr("specs_value_id")) || 0);
        });
        var spec_string = curSpec.sort(function(a, b) { return a - b; }).join("|");
        //获取商品ID
        goods_id = myData.spec_list[spec_string];
        get_detail(goods_id);
    }

    /**
     * 重组img 图片
     * @private
     */
    function _initImage(){
         $html =  $(".fixed-tab-pannel").html();
         //匹配图片（g表示匹配所有结果i表示区分大小写）
         var imgReg = /<img.*?(?:>|\/>)/gi;
         var arr = $html.match(imgReg);
         var srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/i;
         var ImageSrc = [];
         for (var i = 0; i < arr.length; i++) {
            src = arr[i].match(srcReg);
            ImageSrc.push(src);
         }
         for (var i=0;i<arr.length;i++){
              _temp = '<div class="my-gallery" data-pswp-uid="2"><figure><div class="thumb-img"><a href="'+ImageSrc[i][1]+'" data-size="400x400"><img style="height:100%;" src="'+ImageSrc[i][1]+'"></a></div></figure></div>';
             $html = $html.replace(arr[i],_temp);
         }
         $(".fixed-tab-pannel").html($html);
         // execute above function
         initPhotoSwipeFromDOM('.my-gallery');
    }

    $('#goodsDetail').click(function(){
        window.location.href = WapSiteUrl+'/tmpl/product_detail.html?goods_id=' + goods_id;
    });
    $('#goodsBody').click(function(){
        window.location.href = WapSiteUrl+'/tmpl/product_info.html?goods_id=' + goods_id;
    });
    $('#goodsEvaluation').click(function(){
        window.location.href = WapSiteUrl+'/tmpl/product_eval_list.html?goods_id=' + goods_id;
    });



});